<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ExcelExport\CarExpoter;
use App\User;

use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('用户管理');
            $content->description('...');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑');
            $content->description('...');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新建用户');
            $content->description('...');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                // 在这里添加字段过滤器
                $filter->like('name', 'name');
            });
            $grid->exporter(new CarExpoter());
            $grid->id('ID')->sortable();
            $grid->name('姓名')->badge();
            $grid->account('账号');
            $grid->created_at('创建时间');
            $grid->filter(function ($filter) {
                // 设置created_at字段的范围查询
                $filter->between('created_at', 'Created Time')->datetime();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {

//            $form->display('id', 'ID');
//
//            $form->display('created_at', 'Created At');
//            $form->display('updated_at', 'Updated At');
                $form->hidden('id', 'ID');
                $form->text('name', '姓名')->rules('required|max:10');
                $form->hidden('created_at', 'Created At');
                $form->hidden('updated_at', 'Updated At');
                $form->text('account', '账号')->rules('required|max:26');
                $form->password('password', '密码')->rules('required|confirmed');
                $form->password('password_confirmation', '确认密码')->rules('required')
                    ->default(function ($form) {
                        return $form->model()->password;
                    });
                $form->ignore(['password_confirmation']);
                $form->saving(function (Form $form) {
                    if ($form->password && $form->model()->password != $form->password) {
                        $form->password = bcrypt($form->password);
                    }
                });
        });
    }
}
